package com.witframework.plugin.jbossrules.support;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.drools.RuleBase;
import org.drools.RuleBaseFactory;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;

import com.witframework.plugin.jbossrules.RuleBaseLoader;


/**
 * 抽象的Rule文件载入类
 *
 */
public abstract class AbstractFileRuleBaseLoader implements RuleBaseLoader {
	protected boolean isChineseValid=false;
	protected String fileCharset=null;
	/**
	 * RuleBase
	 */
	RuleBase ruleBase = RuleBaseFactory.newRuleBase();

	/**
	 * 支持通配符形式的规则文件定义
	 */
	protected String[] ruleFiles;

	/**
	 * Spring的Resource Loader
	 */
	protected List hasLoadRules=new ArrayList();

	protected ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(
			this.getClass().getClassLoader());

	public void setRuleFiles(String ruleFiles[]) {
		this.ruleFiles = ruleFiles;
	}
	/**
	 * 设置是否让dsl支持中文，这种支持是有限的，详细信息参看开发文档。
	 * @param isValid
	 * @param charset
	 */
	public void setChineseValid(boolean isValid,String charset){
		isChineseValid=isValid;
		fileCharset=charset;
	}
	/**
	 * 分割以，号分格的rule file定义,并用Spring的Resource resolver 匹配资源
	 */
	protected Resource[] getRuleFiles(String[] files) throws IOException {
		List resourceList = new ArrayList();
		for (int i=0;i<files.length;i++) {
			if(files[i].startsWith("/")){
				Resource[] resources = getResources(files[i]);
				resourceList.addAll(Arrays.asList(resources));
			}else{
				FileSystemResource is=new FileSystemResource(files[i]);
				//Resource r=(Resource)is;
				resourceList.add(is);
			}

		}
		Resource[] resources= (Resource[]) resourceList.toArray(new Resource[0]);
		for (int i = 0; i < resources.length; i++) {
			ResourceInfo info=new ResourceInfo();
			info.name=resources[i].getFilename();
			info.lastModify=0;
			hasLoadRules.add(info);
		}
		return resources;
	}

	/**
	 * 使用Spring的Resource resolver 匹配资源
	 */
	protected Resource[] getResources(String s) throws IOException {
		return resolver.getResources(s);
	}
	protected boolean resourceIsModify(Resource r){
		for (int i = 0; i < hasLoadRules.size(); i++) {
			ResourceInfo info =(ResourceInfo)hasLoadRules.get(i);
			try {
				if(r.getFilename().equals(info.name)&&
					r.getFile().lastModified()>info.lastModify){
					return true;
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return false;
	}
	protected void updateResourceInfo(Resource r){
		for (int i = 0; i < hasLoadRules.size(); i++) {
			ResourceInfo info =(ResourceInfo)hasLoadRules.get(i);
			if(info.name.equals(r.getFilename())){
				try {
					info.lastModify=r.getFile().lastModified();
					System.out.println("ruleBase update!!!"+new java.util.Date(info.lastModify));
				} catch (IOException e) {
					e.printStackTrace();
				}
				break;
			}
		}
	}
	class ResourceInfo{
		String name;
		long lastModify;
	}
}
